<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-form-panel no-padding no-border" [formGroup]="mobileActionFormGroup">
  <div class="tb-form-row">
    <div class="fixed-title-width">{{ 'widget-action.mobile.action-type' | translate }}*</div>
    <mat-form-field fxFlex appearance="outline" subscriptSizing="dynamic">
      <mat-select required formControlName="type" placeholder="{{ 'widget-action.mobile.select-action-type' | translate }}">
        <mat-option *ngFor="let actionType of mobileActionTypes" [value]="actionType">
          {{ mobileActionTypeTranslations.get(mobileActionType[actionType]) | translate }}
        </mat-option>
      </mat-select>
      <mat-icon matSuffix
                matTooltipPosition="above"
                matTooltipClass="tb-error-tooltip"
                [matTooltip]="'widget-action.mobile.action-type-required' | translate"
                *ngIf="mobileActionFormGroup.get('type').hasError('required')
                       && mobileActionFormGroup.get('type').touched"
                class="tb-error">
        warning
      </mat-icon>
    </mat-form-field>
  </div>
  <ng-container [formGroup]="mobileActionTypeFormGroup" [ngSwitch]="mobileActionFormGroup.get('type').value">
    <ng-template [ngSwitchCase]="mobileActionFormGroup.get('type').value === mobileActionType.mapDirection ||
                                 mobileActionFormGroup.get('type').value === mobileActionType.mapLocation ?
                                 mobileActionFormGroup.get('type').value : ''">
      <tb-js-func
        formControlName="getLocationFunction"
        functionName="getLocation"
        [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
        [globalVariables]="functionScopeVariables"
        [editorCompleter]="customActionEditorCompleter"
        hideBrackets
        helpId="widget/action/mobile_get_location_fn"
      ></tb-js-func>
    </ng-template>
    <ng-template [ngSwitchCase]="mobileActionType.makePhoneCall">
      <tb-js-func
        formControlName="getPhoneNumberFunction"
        functionName="getPhoneNumber"
        [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
        [globalVariables]="functionScopeVariables"
        [editorCompleter]="customActionEditorCompleter"
        hideBrackets
        helpId="widget/action/mobile_get_phone_number_fn"
      ></tb-js-func>
    </ng-template>
    <ng-template [ngSwitchCase]="mobileActionFormGroup.get('type').value === mobileActionType.takePhoto ||
                                 mobileActionFormGroup.get('type').value === mobileActionType.takePictureFromGallery ||
                                 mobileActionFormGroup.get('type').value === mobileActionType.takeScreenshot ?
                                 mobileActionFormGroup.get('type').value : ''">
      <tb-js-func
        formControlName="processImageFunction"
        functionName="processImage"
        [functionArgs]="['imageUrl', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
        [globalVariables]="functionScopeVariables"
        [editorCompleter]="customActionEditorCompleter"
        hideBrackets
        helpId="widget/action/mobile_process_image_fn"
      ></tb-js-func>
    </ng-template>
    <ng-template [ngSwitchCase]="mobileActionType.scanQrCode">
      <tb-js-func
        formControlName="processQrCodeFunction"
        functionName="processQrCode"
        [functionArgs]="['code', 'format', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
        [globalVariables]="functionScopeVariables"
        [editorCompleter]="customActionEditorCompleter"
        hideBrackets
        helpId="widget/action/mobile_process_qr_code_fn"
      ></tb-js-func>
    </ng-template>
    <ng-template [ngSwitchCase]="mobileActionType.getLocation">
      <tb-js-func
        formControlName="processLocationFunction"
        functionName="processLocation"
        [functionArgs]="['latitude', 'longitude', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
        [globalVariables]="functionScopeVariables"
        [editorCompleter]="customActionEditorCompleter"
        hideBrackets
        helpId="widget/action/mobile_process_location_fn"
      ></tb-js-func>
    </ng-template>
    <ng-template [ngSwitchCase]="mobileActionFormGroup.get('type').value === mobileActionType.mapDirection ||
                                 mobileActionFormGroup.get('type').value === mobileActionType.mapLocation ||
                                 mobileActionFormGroup.get('type').value === mobileActionType.makePhoneCall ?
                                 mobileActionFormGroup.get('type').value : ''">
      <tb-js-func
        formControlName="processLaunchResultFunction"
        functionName="processLaunchResult"
        [functionArgs]="['launched', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
        [globalVariables]="functionScopeVariables"
        [editorCompleter]="customActionEditorCompleter"
        hideBrackets
        helpId="widget/action/mobile_process_launch_result_fn"
      ></tb-js-func>
    </ng-template>
  </ng-container>
  <tb-js-func *ngIf="mobileActionFormGroup.get('type').value"
              formControlName="handleEmptyResultFunction"
              functionName="handleEmptyResult"
              [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
              [globalVariables]="functionScopeVariables"
              [editorCompleter]="customActionEditorCompleter"
              hideBrackets
              helpId="widget/action/mobile_handle_empty_result_fn"
  ></tb-js-func>
  <tb-js-func *ngIf="mobileActionFormGroup.get('type').value"
              formControlName="handleErrorFunction"
              functionName="handleError"
              [functionArgs]="['error', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
              [globalVariables]="functionScopeVariables"
              [editorCompleter]="customActionEditorCompleter"
              hideBrackets
              helpId="widget/action/mobile_handle_error_fn"
  ></tb-js-func>
</div>
